//
//  ImageViewController.m
//  练习-相册
//
//  Created by Jorn.Wu on 15/7/13.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ImageViewController.h"

#define JWScreenWidth [UIScreen mainScreen].bounds.size.width
#define JWScreenHeight [UIScreen mainScreen].bounds.size.height

CGFloat JWimgCount = 10;


@interface ImageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageCtrl;
@property (nonatomic, strong) UIImageView *chengedImg;
@property (nonatomic, strong) UIScrollView *chengedScroll;
@property (nonatomic, assign) CGPoint imgCenter;

@end


@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    [self addScrollView];
    [self addImages];
    [self addItems];
}
- (void)addItems{
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.rightBarButtonItem = editBtn;
    
    UIBarButtonItem *actionBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    UIBarButtonItem *flexibleSpacebtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *trashBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
    
    self.toolbarItems = @[actionBtn,flexibleSpacebtn,trashBtn];
    
}

- (void)addScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth ,JWScreenHeight)];
    
    _scrollView.contentSize = CGSizeMake(JWScreenWidth * JWimgCount, JWScreenHeight - 64 - 44);
    _scrollView.contentOffset = CGPointMake(JWScreenWidth * (_clickTag - 100), 0);//设置初始显示的图片
    self.title = _imageNameAr[_clickTag - 100];
    
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:_scrollView];
    _scrollView.delegate = self;
}
- (void)addImages{

    for (int i = 0; i < JWimgCount; i++) {
        UIScrollView *boreView = [[UIScrollView alloc] initWithFrame:CGRectMake(JWScreenWidth * i, 0, JWScreenWidth, JWScreenHeight - 64 - 44)];
        boreView.backgroundColor = [UIColor whiteColor];
        boreView.contentSize = CGSizeMake(JWScreenWidth, JWScreenHeight - 64 - 44);
        boreView.tag = 100 + i;
        boreView.showsVerticalScrollIndicator = NO;
        boreView.showsHorizontalScrollIndicator = NO;
        boreView.scrollEnabled = NO;
        
        //缩放比例控制
        boreView.minimumZoomScale = 0.5;
        boreView.maximumZoomScale = 5;
        boreView.delegate = self;
        
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight - 64 - 44)];
        UIButton *btn = _imageAr[i];
        imgView.image = btn.imageView.image;
        
        [boreView addSubview:imgView];
        
        [_scrollView addSubview:boreView];
    }
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    NSInteger CurrentTag = _scrollView.contentOffset.x / JWScreenWidth + 100;
    _chengedScroll = (UIScrollView *)[scrollView viewWithTag:CurrentTag];
    _chengedImg = [scrollView viewWithTag:CurrentTag].subviews[0];
    
    return _chengedImg;//变动的图片
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    _imgCenter = _chengedImg.center;//保存中心点
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    _chengedImg.center = _imgCenter;//保持中心点不变
    
}
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//    if (scrollView == _scrollView) {
//    
//    }
//}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    if (scrollView == _scrollView) {
        [_chengedScroll setZoomScale:1];//将改变尺寸的图片还原尺寸
        
        NSInteger index = (NSInteger)scrollView.contentOffset.x / JWScreenWidth;//
        self.title = _imageNameAr[index];
    }
}

@end
